<?php
if (!empty ($detail)){
 echo "<h2>Mengundang Peserta</h2>";
?>
<table>
    <tr>
        <td>Nama Kegiatan</td>
        <td>:</td>
        <td><?php echo $detail->activityName;?></td>
    </tr>

     <tr>
        <td>Ruangan</td>
        <td>:</td>
        <td><?php echo $detail->roomID;?></td>
    </tr>
     <tr>
        <td>Hari </td>
        <td>:</td>
        <td><?php echo $detail->day;?></td>
    </tr>
        <tr>
        <td>Tanggal  </td>
        <td>:</td>
        <td><?php echo $detail->startDate;?>-<?php echo $detail->endDate;?></td>
    </tr>
     <tr>
        <td>Waktu</td>
        <td>:</td>
        <td><?php echo $detail->startTime;?>-<?php echo $detail->endTime;?></td>
    </tr>
     <tr>
        <td>PJ Kegiatan</td>
        <td>:</td>
        <td><?php echo $detail->pjKegiatan;?></td>
    </tr>
</table>

<?php } ?>
<form method="post" action="<?php echo site_url('ServerMengundangPengguna/save') ?>" onsubmit='return false'>
    <input type='hidden' name='activity_id' value='<?php echo $activity_id ?>'/>
<!--    <input type='hidden' name='activity_type' value='<?php //echo $activity_type ?>'/> -->
    <h2>Masukkan Nama Peserta</h2>
    <input type="text" name="nama_peserta" id="nama_peserta"/>
    <table>
        <thead>
            <tr>
                <th>Peserta Ke</th>
                <th>Nama Peserta</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody id="daftar_peserta">
        <?php $i = 0;
            if (!empty($peserta)): ?>
<?php foreach ($peserta as $row): ?>
                    <tr>
                        <td><?php echo++$i; ?></td>
                        <td><?php echo $row['name'] ?></td>
                        <td><a rel='facebox' href='<?php echo site_url('ServerMengundangPengguna/view/' . $activity_id . '/' . $row['userID']) ?>'>View</a> | <a href='<?php echo site_url('ServerMengundangPengguna/ajaxHapusPeserta/' . $activity_id . '/' . $row['userID']) ?>'>Delete</a></td>
                    </tr>
            <?php endforeach; ?>
<?php endif; ?>
                </tbody>
            </table>
    <div>
        <label>Jumlah Peserta : </label><span id="jumlah_peserta"><?php echo !empty($peserta)?count($peserta):0?></span>
    </div>
        </form>
        <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/jquery-autocomplete/jquery.autocomplete.css" />
        <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/facebox/src/facebox.css" />
        <script type="text/javascript" src="<?php echo base_url(); ?>assets/jquery-ui-1.8.11.custom/js/jquery-1.5.1.min.js"> </script>
        <script type="text/javascript" src="<?php echo base_url(); ?>assets/jquery-autocomplete/jquery.autocomplete.min.js"> </script>
        <script type="text/javascript" src="<?php echo base_url(); ?>assets/facebox/src/facebox.js"> </script>
        <script type="text/javascript">
            var i = <?php echo $i ?>;
            function makeFacebox(){
                $('a[rel*=facebox]').facebox();
            }
            
            jQuery(function(){
                makeFacebox();
                
                $("#nama_peserta").autocomplete('<?php echo site_url('ServerMengundangPengguna/ajaxNamaPeserta/' . $activity_id . '/') ?>',{
                    dataType : 'json',
                    noCache : true,
                    parse : function (data){
                        var array = new Array();
                        for(var i=0;i<data.length;i++)
                        {
                            array[array.length] = { data: data[i], value: data[i], result: data[i].name };
                        }
                        return array;

                    },
                    formatItem : function (row){
                        return row.name;
                    }
                }).result(function(event, item){
                    $.ajax({
                        url: "<?php echo site_url('ServerMengundangPengguna/ajaxTambahPeserta/' . $activity_id) ?>/" + item.userID +'<?php //echo $activity_type ?>',
                        success: function(response){
                            if(response){
                                i++;
                                html = '<tr>';
                                html += '<td>'+ i +'</td>';
                                html += '<td>' + item.name + '<input type="hidden" name="user_id[' + i +']" value="' + item.userID + '"/></td>';
                                html += "<td><a rel='facebox' href='<?php echo site_url('ServerMengundangPengguna/view/' . $activity_id) ?>/" + item.userID + "'>View</a> | ";
                                html += "<a href='<?php echo site_url('ServerMengundangPengguna/ajaxHapusPeserta/' . $activity_id); ?>/" + item.userID + "'>Delete</a></td>";
                        html += '</tr>';
                        $('#daftar_peserta').append(html);
                        $('#nama_peserta').val('');
						$('#nama_peserta').flushCache();
                        $('#jumlah_peserta').html(parseInt($('#jumlah_peserta').html()) + 1 );
                        makeFacebox();
                    }
                    else{
                        alert('Tidak bisa menamahkan peserta');
                    }
                }
            });
        });
    });
</script>